<template>
  <div id="firstpage">
    <div class="top">
      <div class="search">
        <div class="left" @click="changeCity">
          <img src="@/assets/img/icon-address.png" alt="" />
          <span class="spills">{{ cityName }} </span>
        </div>
        <div class="right">
          <van-button size="small " color="#23a8fc" type="primary"
            >搜&nbsp;&nbsp;&nbsp;&nbsp;索</van-button
          >
          <van-search
            v-model="value"
            shape="round"
            background="#23a8fc"
            placeholder="请输入搜索关键词"
          />
        </div>
      </div>
      <div class="nav">
        <span>每日推荐</span>
        <span>游乐场</span>
        <span>海滨沙滩</span>
        <span>山川之旅</span>
      </div>
    </div>
    <div class="main">
      <div class="part1">
        <div class="grid">
          <router-link to="/trip/airplane" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span>&#xe602;</span>
              <p>机票</p>
            </div>
          </router-link>
          <router-link to="/trip/ship" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span>&#xe678;</span>
              <p>车/船</p>
            </div>
          </router-link>
          <router-link to="/trip/train" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span> &#xe66a;</span>
              <p>火车</p>
            </div>
          </router-link>
          <router-link to="/hotel" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span> &#xe646; </span>
              <p>住宿</p>
            </div>
          </router-link>
          <router-link to="/trip/taxi" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span>&#xe62f;</span>
              <p>接租</p>
            </div>
          </router-link>
          <router-link to="/travel" custom v-slot="{ navigate }">
            <div class="iconfont" @click="navigate">
              <span> &#xe657; </span>
              <p>旅游</p>
            </div>
          </router-link>
        </div>
        <div class="shop iconfont">
          &#xf0135; <br />
          购 <br />
          物
        </div>
      </div>
      <div class="part2">
        <div class="banner">
          <h3>精选榜 <span>更多</span></h3>
          <div>
            <div v-for="item in bannerList" :key="item.id">
              <div>
                <img :src="getPictureSrc(item.img)" alt="" />
              </div>
              <p>{{ item.name }}</p>
            </div>
          </div>
        </div>
      </div>
      <div class="part3">
        <img src="@/assets/img/firstpagebanner.png" alt="" />
        <p>追逐梦想的你，寻找着诗和远方</p>
      </div>
      <div class="part4">
        <div class="left">
          <div v-for="item in mainListL" :key="item.id">
            <div class="img">
              <img :src="item.img" alt="" />
            </div>
            <h3>{{ item.title }}</h3>
            <div class="text" v-if="item.name">
              <p>
                <img src="@/assets/img/firstpageTouxiang.png" alt="" />
                {{ item.name }}
              </p>
              <p>
                <img src="@/assets/img/icon-eye.png" alt="" />{{ item.count }}
              </p>
            </div>
          </div>
        </div>
        <div class="right" v-if="mainListR">
          <div v-for="item in mainListR" :key="item.id">
            <div class="img">
              <img :src="item.img" alt="" />
            </div>
            <h3>{{ item.title }}</h3>
            <div class="text" v-if="item.name">
              <p>
                <img src="@/assets/img/firstpageTouxiang.png" alt="" />
                {{ item.name }}
              </p>
              <p>
                <img src="@/assets/img/icon-eye.png" alt="" />{{ item.count }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="list">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="immediateCheck"
      >
        <van-cell v-for="item in list" :key="item" />
      </van-list>
    </div>
  </div>
</template>
<script>
import { computed, ref } from "vue";
import { List, Search, Button, Cell } from "vant";
import "@/assets/icon-font-firstpage/iconfont.css";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
export default {
  name: "FirstPage",
  components: {
    [List.name]: List,
    [Search.name]: Search,
    [Button.name]: Button,
    [Cell.name]: Cell,
  },
  setup() {
    let text = ref("文字");
    let value = ref("");
    //引入路由
    const router = useRouter();
    //引入仓库
    const store = useStore();
    //为修改城市添加事件
    function changeCity() {
      router.push("/city");
    }
    // 获取本地的存储的地址
    const cityName = computed(() => {
      return localStorage.getItem("cityName") || "杭州";
    });
    //请求数据，存到仓库中
    store.dispatch("firstPage/firstPageData");
    setTimeout(() => {
      store.commit("firstPage/getFirst");
    }, 300);
    //首页数据渲染
    let bannerList = computed(() => store.state.firstPage.bannerList);
    let mainListL = computed(() => store.state.firstPage.mainListL);
    let mainListR = computed(() => store.state.firstPage.mainListR);
    //修改图片url
    function getPictureSrc(url) {
      if (url) {
        let reg = /img\/(.*).png$/;
        let str = url;
        let res = str.match(reg)[1];
        return require("../../assets/img/" + res + ".png");
      }
    }
    // vant List 组件
    const list = computed(() => store.state.firstPage.mainListL);
    const loading = ref(false);
    const finished = ref(false);
    // 是否在初始化时立即执行滚动位置检查
    const immediateCheck = ref(true);
    const onLoad = () => {
      // 异步更新数据
      setTimeout(() => {
        store.commit("firstPage/getMore");
        // 加载状态结束
        loading.value = false;
        // 数据全部加载完成
        if (list.value.length >= 6) {
          finished.value = true;
        }
      }, 1000);
    };
    return {
      text,
      value,
      bannerList,
      mainListL,
      mainListR,
      getPictureSrc,
      changeCity,
      cityName,
      list,
      onLoad,
      loading,
      finished,
      immediateCheck,
    };
  },
};
</script>
<style lang="less" scoped>
p {
  margin: 0;
  padding: 0;
}
#firstpage {
  > .top {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: #23a8fc;
    color: white;
    z-index: 20;
    > .search {
      display: flex;
      padding: 0 22px;
      align-items: center;
      .left {
        > span {
          font-size: 15.5px;
          margin: 0 10px;
          width: 50px;
          display: inline-block;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
      .right {
        > button {
          position: fixed;
          right: 35px;
          top: 11px;
          border-radius: 30px;
        }
        flex: 1;
      }
    }
    .nav {
      width: 100%;
      height: 44px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      > span {
        display: block;
        width: 70px;
        height: 25px;
        font-size: 12.5px;
        border-radius: 12px;
        color: white;
        background-color: #6dc6ff;
        text-align: center;
        line-height: 25px;
        text-align: center;
      }
    }
  }
  > .main {
    .part1 {
      margin: 0 22px;
      border-radius: 20px;
      margin-top: 113px;
      background-color: lightblue;
      overflow: hidden;
      display: flex;
      > .grid {
        width: 300px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        flex-wrap: wrap;
        > div {
          width: 33%;
          height: 54px;
          padding: 16px 8px;
          text-align: center;
          box-sizing: border-box;
          padding: 10px;
          color: white;
          background-color: #009afc;
          border: white solid 1px;
          > p {
            font-size: 16px;
          }
        }
      }
      > .shop {
        color: white;
        text-align: center;
        vertical-align: middle;
        width: 72px;
        background-color: #009afc;
        padding: 25px 0;
      }
    }
    > .part2 {
      padding: 0 22px;
      overflow: hidden;
      > .banner {
        width: 100%;
        overflow: auto;
        > h3 {
          font-weight: 800;
          font-size: 17px;
          font-family: PingFang-SC-Heavy;
          color: #101010;
          line-height: 17px;
          height: 17px;
          margin-left: 15.5px;
          > span {
            float: right;
            display: block;
            font-size: 14px;
            font-weight: normal;
            margin-left: 23px;
          }
        }
        > div {
          width: calc(200%-44px);
          display: flex;
          overflow: auto;
          > div {
            margin: 0 11px;
            > div {
              width: 70px;
              height: 95px;
              border-radius: 10px;
              overflow: hidden;
              > img {
                width: 70px;
                height: 95px;
              }
            }
            > p {
              color: #101010;
              font-size: 10px;
              line-height: 13.5px;
              padding: 8px 0;
            }
          }
        }
      }
    }
    > .part3 {
      position: relative;
      margin: 20px;
      width: calc(100% -40px);
      height: 70px;
      border-radius: 35px;
      overflow: hidden;
      > img {
        display: block;
        width: 100%;
        height: 70px;
      }
      > p {
        margin: 0;
        padding: 0;
        position: absolute;
        top: 40%;
        left: 50%;
        transform: translate(-50%);
        font-size: 17.5px;
        color: white;
        width: auto;
        white-space: nowrap;
        height: 50px;
      }
    }
    > .part4 {
      padding: 0 22px;
      display: flex;
      justify-content: space-between;
      > .left,
      > .right {
        > div {
          > .img {
            width: 175px;
            border-radius: 15px 15px 0 0;
            overflow: hidden;
            img {
              width: 175px;
            }
          }
          > h3 {
            color: #101010;
            font-size: 13.5px;
          }
          > .text {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            > p {
              display: flex;
              align-items: center;
              vertical-align: middle;
              color: #909090;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
  > .bottom {
    text-align: center;
    color: #c5c5c5;
    font-family: PingFang-SC-Medium;
    font-size: 14.5px;
    margin: 33px 0;
  }
}
</style>
